package com.cdpm.web.controller.system;

import com.cdpm.common.core.controller.BaseController;
import com.cdpm.common.core.domain.R;
import com.cdpm.common.core.page.TableDataInfo;
import com.cdpm.system.domain.SysArticle;
import com.cdpm.system.domain.vo.SysArticleRes;
import com.cdpm.system.domain.vo.SysArticleUpVO;
import com.cdpm.system.domain.vo.SysArticleVO;
import com.cdpm.system.service.ISysArticleService;
import com.cdpm.web.dto.ENUM_CHANNEL_TYPE;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;
import java.util.List;

/**
 * 文章
 *
 * @author zf
 * @since 2024-08-16 11:11:23
 */
@Api(tags = "文章")
@RestController
@RequestMapping("/system/sysArticle")
public class SysArticleController extends BaseController {

    @Autowired
    private ISysArticleService sysArticleService;

    /**
     * 列表查询
     *
     * @param sysArticleVO
     * @return
     */
    @PostMapping("/findList")
    @ApiOperation(value = "列表查询")
    public TableDataInfo findList(@RequestBody SysArticleVO sysArticleVO) {
        startPage();
        List<SysArticleRes> result = sysArticleService.findList(sysArticleVO);
        result.forEach(sysArticle -> {
            sysArticle.setChannelName(ENUM_CHANNEL_TYPE.getLabelByValue(sysArticle.getChannelCode()));
        });
        return getDataTable(result);
    }

    /**
     * 查询
     *
     * @param articleId
     * @return
     */
    @GetMapping("findById")
    @ApiOperation(value = "详情")
    public R<SysArticle> findById(@RequestParam("articleId") Long articleId) {
        SysArticle sysArticle = sysArticleService.findById(articleId);
        return R.ok(sysArticle);
    }

    /**
     * 新增
     *
     * @param sysArticle
     * @return
     */
    @PostMapping("/insertSysArticle")
    @ApiOperation(value = "新增")
    public R insert(@RequestBody SysArticle sysArticle) {
        sysArticleService.insertSysArticle(sysArticle);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param sysArticle
     * @return
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改")
    public R update( @RequestBody SysArticle sysArticle) {
         sysArticleService.update(sysArticle);
        return R.ok();
    }

    /**
     * 关闭或上架
     *
     * @param sysArticleUpVO
     * @return
     */
    @PostMapping("closeOrUp")
    @ApiOperation(value = "关闭或上架文章")
    public R closeOrUp(@RequestBody SysArticleUpVO sysArticleUpVO) {
        sysArticleService.closeOrUp(sysArticleUpVO);
        return R.ok();
    }

}

